package online.inote.dic.api.dic;

import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import online.inote.dic.api.base.BaseController;
import online.inote.dic.common.annotions.Valid;
import online.inote.dic.common.utils.ResultFactory;
import online.inote.dic.model.dic.DicEntity;
import online.inote.dic.validator.dic.DicValidator;

/**
 * @description: 字典Controller
 * @author XQF.Sui
 * @created 2019年6月10日 下午11:11:21
 * @version 1.0
 */
@RestController
@RequestMapping(value = "api/dic")
public class DicController extends BaseController<DicEntity, String> {

	/**
	 * @description 保存字典信息
	 * @author XQF.Sui
	 * @created 2019年6月10日 下午11:23:04
	 * @param dic 字典信息
	 * @return
	 */
	@PostMapping(value = "save")
	@Valid(validator =DicValidator.class)
	public ResultFactory<DicEntity> save(@RequestBody DicEntity dic) {

		if (StringUtils.isBlank(dic.getId())) {	// 新增
			dic.setId(UUID.randomUUID().toString());
			service.insert(dic);
		} else { // 修改
			service.updateByPrimaryKey(dic);
		}
		
		return ResultFactory.success();
	}
	
	/**
	 * @description 更改字典状态
	 * @author XQF.Sui
	 * @created 2019年7月16日 下午9:25:48
	 * @param dic 字典信息
	 * @return
	 */
	@PostMapping(value = "updateStatus")
	@Valid(validator =DicValidator.class)
	public ResultFactory<DicEntity> updateStatus(@RequestBody DicEntity dic) {
		service.updateByPrimaryKey(dic);
		return ResultFactory.success();
	}
}